<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="`never`">
        <div slot="header">
          <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>学科管理</el-breadcrumb-item>
            <el-breadcrumb-item>目录管理</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <el-row>
          <el-col :span="20">
            <el-row>
              <span style="font-size: 14px; font-weight: bold; margin-right: 10px">目录名称</span>
              <el-input style="width: 200px" v-model="params.directoryName"></el-input>
              <span style="font-size: 14px; font-weight: bold; margin: 0 10px 0 40px">状态</span>
                <el-select v-model="params.state" placeholder="请选择">
                  <el-option label="启用" value="1"></el-option>
                  <el-option label="禁用" value="0"></el-option>
                </el-select>
                <!-- 按钮 -->
              <el-button style='margin-left: 10px' size="small" @click="clear()">清空</el-button>
              <el-button size="small" type="primary" @click="search()">搜索</el-button>
            </el-row>
          </el-col>
          <!-- 新增按钮 -->
          <el-col :span="4" style="text-align: right">
            <el-button  type="text" icon="el-icon-back" @click="$router.push('/subjects/list')">返回学科</el-button>
            <el-button type="success" icon="el-icon-edit" size="small" @click="dialogOpen">新增目录</el-button>
          </el-col>
        </el-row>
        <!-- 小tips -->
        <el-alert
          style="margin-top: 10px; margin-bottom: 10px"
          :title="`数据一共 ${ total }条`"
          type="info"
          :closable="false"
          show-icon>
        </el-alert>
        <!-- 列表 -->
        <el-table
          :header-cell-style="{background: '#fafafa'}"
          :data="directorysList" style="width: 100%;">
          <el-table-column label="序号" type="index" width="70" />
          <el-table-column label="所学科目" prop="subjectName" width="120" />
          <el-table-column label="目录名称" prop="directoryName" />
          <el-table-column label="创建者" prop="username" />
          <el-table-column label="创建日期" prop="addDate">
            <template #default="{ row }">
              {{ formatDate(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column label="面试题靠数量" prop="totals" />
          <el-table-column label="状态" prop="state" width="90">
            <template #default="{ row }">
              {{ row.state === 1 ? '已启用' : '已禁用' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200">
            <template #default="{ row }">
              <el-button type="text" @click="toggleState(row)">{{ row.state === 1 ? '禁用' : '启用'}}</el-button>
              <el-button type="text" @click="revise(row)" :disabled="row.state === 1">修改</el-button>
              <el-button type="text" @click="delDirectory(row)" :disabled="row.state === 1">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页功能 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="params.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="params.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total"
          background
          style="margin-top:20px; text-align: right">
        </el-pagination>
      </el-card>
    </div>
    <!-- 新增、修改目录弹窗 -->
    <directorys-add
      ref="directorysDialogRef"
      :visible="dialogVisible"
      @dialog-close="dialogClose"
      @fetch-directorys-list="fetchDirectorysList"
    ></directorys-add>
  </div>
</template>

<script>
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
import dayjs from 'dayjs'
import DirectorysAdd from '../components/directorys-add.vue'

export default {
  components: {
    list,
    DirectorysAdd
  },
  data() {
    return {
      input: '',
      select: '',
      directorysList: [],
      params: {
        page: 1, // 当前页数
        pagesize: 10, // 页尺寸
        pages: 0 // 总页数
      },
      total: 0,
      // 控制弹窗的显示与隐藏
      dialogVisible: false,
      subjectOptions: []
    }
  },
  created () {
    getCompanys()
    getQuestion()
    this.fetchDirectorysList()
  },
  methods: {
    // 渲染列表
    async fetchDirectorysList() {
      const res = await list(this.params)
      // console.log(res);
      this.directorysList = res.data.items
      this.total = res.data.counts
      this.pagesizes = res.data.pagesize
      this.pages = res.data.pages
      this.page = res.data.page
    },
    // 每页条数发生变化，触发的回调函数
    handleSizeChange(size) {
      this.params.page = 1
      this.params.pagesize = size
      this.fetchDirectorysList()
    },
    // 当前页发生变化，触发的回调函数
    handleCurrentChange(page) {
      this.params.page = page
      this.fetchDirectorysList()
    },
    // 转换日期格式
    formatDate(date) {
      return dayjs(date).format('YYYY-MM-DD HH:mm:ss ')
    },
    // 清空输入框
    clear() {
      this.params = {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 搜索输入框内容
    search() {
      this.params.page= 1
      this.fetchDirectorysList()
    },
    // 切换状态
    async toggleState(data) {
      // 切换状态
      data.state = data.state === 1 ? 0 : 1
      // 变量用 let
      let param = {
        id: data.id,
        state: data.state
      }
      await changeState(param)
      // 提示用户
      this.$message.success('操作成功')
    },
    // 打开弹窗
    dialogOpen() {
      this.dialogVisible = true
    },
    // 弹窗关闭
    dialogClose() {
      this.dialogVisible = false
    },
    // 修改数据
    revise(id) {
      this.dialogVisible = true
      // 数据回显
      this.$refs.directorysDialogRef.fetchdirectorysDetail(id)
    },
    // 删除数据
    delDirectory(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await remove(id)
          this.$message.success("删除成功")
          this.fetchDirectorysList()
        })
    }
  }
}
</script>

<style scoped>

</style>
